//
//  YCTextChannelCollectionCell.m
//  ChannelChoose
//
//  Created by 唐可雄 on 2019/7/27.
//  Copyright © 2019 swustyc. All rights reserved.
//

#import "YCTextChannelCollectionCell.h"

@interface YCTextChannelCollectionCell ()

@property (nonatomic, strong) CAShapeLayer *borderLayer;

@end

@implementation YCTextChannelCollectionCell

#pragma mark - UIView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self configView];
    }
    return self;
}

#pragma mark - View Config

- (void)configView {
    self.translatesAutoresizingMaskIntoConstraints = NO;
    self.userInteractionEnabled = YES;
    self.layer.cornerRadius = 5.f;
    
    self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    self.textLabel.textColor = [self textColorNotFixed];
    self.textLabel.adjustsFontSizeToFitWidth = YES;
    self.textLabel.userInteractionEnabled = YES;
    self.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.textLabel];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1 constant:0]];
    [self.contentView  addConstraint:[NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
    
    self.borderLayer = [CAShapeLayer layer];
    self.borderLayer.bounds = self.bounds;
    self.borderLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    self.borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.borderLayer.bounds cornerRadius:self.layer.cornerRadius].CGPath;
    self.borderLayer.lineWidth = 1;
    self.borderLayer.lineDashPattern = @[@5, @3];
    self.borderLayer.fillColor = [UIColor clearColor].CGColor;
    self.borderLayer.strokeColor = [self backgroundColorNotMoving].CGColor;
    [self.layer addSublayer:self.borderLayer];
    
    self.isMoving = NO;
}

#pragma mark - Color

- (UIColor *)backgroundColorNotMoving {
    return [UIColor colorWithRed:241/255.0f green:241/255.0f blue:241/255.0f alpha:1];
}

- (UIColor *)textColorNotFixed {
    return [UIColor colorWithRed:40/255.0f green:40/255.0f blue:40/255.0f alpha:1];
}

-(UIColor*)textColorFixed {
    return [UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];
}

#pragma mark - Property

-(void)setIsMoving:(BOOL)isMoving
{
    _isMoving = isMoving;
    if (_isMoving) {
        self.backgroundColor = [UIColor clearColor];
        self.borderLayer.hidden = NO;
    }else{
        self.backgroundColor = [self backgroundColorNotMoving];
        self.borderLayer.hidden = YES;
    }
}

-(void)setIsFixed:(BOOL)isFixed{
    _isFixed = isFixed;
    if (isFixed) {
        self.textLabel.textColor = [self textColorFixed];
    }else{
        self.textLabel.textColor = [self textColorNotFixed];
    }
}

@end
